<template>
  <PageWrapper contentFullHeight>
    <ListTemplate
      :register-table="registerTable"
      addauth="system:senior:dept:add"
      :method="methods"
    />
    <Handle @register="registerLayer" @success="tableMethods.reload" />
  </PageWrapper>
</template>
<script setup lang="ts">
  import ListTemplate from '@/views/common/operate/listTemplate.vue';

  import ApiV1 from '@/views/common/operate/ApiV1';
  import { createAsyncComponent } from '@/utils/factory/createAsyncComponent';
  import { listToTree } from '@/utils/helper/treeHelper';
  import {
    apiDeleteDeptsDeletePath,
    apiPostDeptsQuery,
  } from '@/api/http/system/senior/depts/depts.api';
  import {
    useDeptsQueryColumns,
    useDeptsQuerySearchForm,
  } from '@/api/http/system/senior/depts/depts.data';
  import PageWrapper from '@/components/Page/src/PageWrapper.vue';

  // const { hasPermission } = usePermission();
  defineOptions({
    name: 'SystemDeptList',
  });
  const Handle = createAsyncComponent(() => import('./handle.vue'));
  const { registerTable, registerLayer, methods, tableMethods } = ApiV1.useList({
    tableProps: {
      api: apiPostDeptsQuery,
      columns: useDeptsQueryColumns(),
      formConfig: {
        schemas: useDeptsQuerySearchForm(),
      },
      rowKey: 'id',
      isTreeTable: true,
      pagination: false,
      afterFetch: (rows) => {
        return listToTree(rows, { id: 'id', pid: 'parentId', children: 'children' });
      },
    },
    editActionItem: {
      auth: 'system:senior:dept:edit',
    },
    delActionItem: {
      auth: 'system:senior:dept:delete',
    },
    mode: 'model',
    delFN: apiDeleteDeptsDeletePath,
    auth: 'system:senior:dept:list',
  });
</script>
